package com.wanmait.happyheating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.happyheating.mapper.MaterialsUseMapper;
import com.wanmait.happyheating.mapper.WorkersMapper;
import com.wanmait.happyheating.pojo.FaultType;
import com.wanmait.happyheating.pojo.MaterialsUse;
import com.wanmait.happyheating.pojo.Ordersinfo;
import com.wanmait.happyheating.mapper.OrdersinfoMapper;
import com.wanmait.happyheating.pojo.Workers;
import com.wanmait.happyheating.service.OrdersinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.happyheating.util.WebSocketWithOrdersUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 订单详情表 服务实现类
 * </p>
 *
 * @author yuQianTao
 * @since 2021-05-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrdersinfoServiceImpl extends ServiceImpl<OrdersinfoMapper, Ordersinfo> implements OrdersinfoService {

    @Resource
    OrdersinfoMapper ordersinfoMapper;
    @Resource
    MaterialsUseMapper materialsUseMapper;
    @Resource
    private WorkersMapper workersMapper;

    @Override
    public List<Ordersinfo> findUserinfoAll() {
        return null;
    }

    @Override
    public List<Ordersinfo> findWorkesById(Integer id) {
        return ordersinfoMapper.finndWorksById(id);
    }

    /**
     * 根据ID查询所有该用户的订单
     */

    @Override
    public PageInfo<Ordersinfo> findUserOrderInfoByPage(Integer pageNum, Integer pageSize,Integer userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Ordersinfo> ordersinfosList=ordersinfoMapper.findUserinfoAll();
        //页码数为7
        PageInfo<Ordersinfo> infoByPage=new PageInfo<>(ordersinfosList,7);
        return infoByPage;
    }

    @Override
    public PageInfo<Ordersinfo> findWorkOrderInfoByPage(Integer pageNum, Integer pageSize, Integer workerId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Ordersinfo> ordersinfosList=ordersinfoMapper.findUserinfoAll();
        //页码数为7
        PageInfo<Ordersinfo> infoByPage=new PageInfo<>(ordersinfosList,3);
        return infoByPage;

    }

    @Override
    public Ordersinfo findById(Integer id) {
        System.out.println(id);
        Ordersinfo ordersinfo=ordersinfoMapper.findById(id);


        if(ordersinfo.getStateId()>1){
            List<MaterialsUse>   materialsUses=materialsUseMapper.findByOrderId(id);
            ordersinfo.setMaterialsUses(materialsUses);
        }


        return ordersinfo;
    }

    @Override
    public void update(Ordersinfo ordersinfo) {
        ordersinfoMapper.save(ordersinfo);
    }

    @Override
    public void updateStateId(Ordersinfo ordersinfo) {
        ordersinfoMapper.updateStateId(ordersinfo);
    }

    @Override
    public Ordersinfo findFirstOrderByEmptyWorker(Integer userinfoId) {
        return ordersinfoMapper.findFirstOrderByEmptyWorker(userinfoId);
    }

    @Override
    public List<Ordersinfo> finndWorksByIdByOrdersinfoId(Integer id, Integer stateId) {
        return ordersinfoMapper.finndWorksByIdByOrdersinfoId(id,stateId);
    }

    @Override
    public List<Ordersinfo> hang(Integer id, Integer stateId) {
        return ordersinfoMapper.hang(id,stateId);
    }
    /**
     * 判断该订单是否已经被维修人员接单
     * @param orderId 目标订单的ID
     * @return 接单：true；未结单：false
     */
    @Override
    public Boolean checkOrderHasWorker(Integer orderId) {
        Ordersinfo ordersinfo = ordersinfoMapper.findById(orderId);
        return ordersinfo.getWorkers().getId() != null;
    }

    @Override
    public void sendDeleteMesToWorkers(Ordersinfo ordersinfo) {
        //获取曾经接收这个订单信息的所有维修人员的集合
        List<Workers> workers = workersMapper.findWorker(ordersinfo.getOrderTime());
        //向上述维修人员发送删除订单的信息
        String deleteKey = "delete"+ordersinfo.getId();
        WebSocketWithOrdersUtils.sendDeleteOrderMesToWorkers(deleteKey,workers);
    }

}
